<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Purchase extends Model
{
    use HasFactory;

    protected $fillable = ['company_id','project_id','admin_id','warehouse_id','name','type','number','encoding','price','specification','brand','unit','applicant','data_cleaning','quantity','delivery_note_number','unit_price','acceptance_photos','delivery_note_photo','inspector','delivery_date','application_number','delivery_unit','remarks','status'];
    const POSTONLY = ['company_id','project_id','admin_id','warehouse_id','name','type','number','encoding','price','specification','brand','unit','applicant','data_cleaning','quantity','delivery_note_number','unit_price','acceptance_photos','delivery_note_photo','inspector','delivery_date','application_number','delivery_unit','remarks','status'];

    protected $appends = [
        'status_text'
    ];

    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    public function getStatusTextAttribute()
    {
        return $this->status == 1 ? '已通过' : '未通过';
    }

    public function approval()
    {
        return $this->hasOne(Approval::class,'source_id','id')->where('model','Purchase')->select('id','source_id','status','approved_at');
    }

    public function resources()
    {
        return $this->hasMany(ResourceBind::class,'source_id','id')->where('model','Purchase')->with('resource');
    }

    public function details()
    {
        return $this->hasMany(PurchaseDetail::class,'purchase_id','id');
    }



}
